from datetime import datetime

from info import db


class BaseModel(object):
    """模型基类，为每个模型补充创建时间与更新时间"""
    create_time = db.Column(db.DateTime, default=datetime.now)  # 记录的创建时间
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)  # 记录的更新时间


class News(BaseModel, db.Model):
    """新闻"""
    __tablename__ = "info_news"

    id = db.Column(db.Integer, primary_key=True)  # 新闻编号
    title = db.Column(db.String(256), nullable=False)  # 新闻标题
    content = db.Column(db.Text, nullable=False)  # 新闻内容
    index_image_url = db.Column(db.String(256))  # 新闻列表图片路径

    def to_dict(self):
        resp_dict = {
            "id": self.id,
            "title": self.title,
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            "content": self.content,
            "index_image_url": self.index_image_url
        }
        return resp_dict


class Product(BaseModel, db.Model):
    # 产品
    __tablename__ = "info_Product"

    id = db.Column(db.Integer, primary_key=True)  # 产品编号
    name = db.Column(db.String(256), nullable=False)  # 产品名称
    content = db.Column(db.Text, nullable=False)  # 产品介绍
    index_image_url = db.Column(db.String(256))  # 产品图片路径

    def to_dict(self):
        resp_dict = {
            "id": self.id,
            "name": self.name,
            # "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            "content": self.content,
            "index_image_url": self.index_image_url
        }
        return resp_dict



class Category(BaseModel, db.Model):
    """施工现场分类"""
    __tablename__ = "category"

    id = db.Column(db.Integer, primary_key=True)  # 分类编号
    name = db.Column(db.String(256), nullable=False)  # 分类名称

    def to_dict(self):
        resp_dict = {
            "id": self.id,
            "name": self.name
        }
        return resp_dict

class Message(BaseModel, db.Model):
    # 产品
    __tablename__ = "info_message"

    id = db.Column(db.Integer, primary_key=True)  # 产品编号
    name = db.Column(db.String(256), nullable=False)  # 产品名称
    mobile = db.Column(db.Integer,nullable=False) #客户电话
    email = db.Column(db.String(256), nullable=False) #客户邮箱
    content = db.Column(db.Text, nullable=False)  # 产品介绍

    def to_dict(self):
        resp_dict = {
            "id": self.id,
            "name": self.name,
            "create_time": self.create_time.strftime("%Y-%m-%d %H:%M:%S"),
            "content": self.content,
            "mobile":self.mobile,
            "email":self.email
        }
        return resp_dict